//
//  CrushUnitSprite.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/8.
//
//

#include "CrushUnitSprite.h"
#include "AttackAction.h"

USING_NS_CC;

#define TOTAL_UNIT (5)
#define TOTAL_UNIT_EX (8)

static const char *unitNormal[TOTAL_UNIT_EX] = {
    "Sword.png",
    "Arrow.png",
    "Fire.png",
    "Light.png",
    "Health.png",
    "JellyMonster.png",
    "Bomb.png",
    "AllUnit.png",
};

static const char *unitVertical[TOTAL_UNIT] = {
    "Sword_V.png",
    "Arrow_V.png",
    "Fire_V.png",
    "Light_V.png",
    "Health_V.png",
    
};

static const char *unitHorizontal[TOTAL_UNIT] = {
    "Sword_H.png",
    "Arrow_H.png",
    "Fire_H.png",
    "Light_H.png",
    "Health_H.png",

};

static const char *unitCircle[TOTAL_UNIT] = {
    "Sword_O.png",
    "Arrow_O.png",
    "Fire_O.png",
    "Light_O.png",
    "Health_O.png",
    
};


float CrushUnitSprite::getContentWidth()
{
    static float itemWidth = 0;
    if (0 == itemWidth) {
        Sprite *sprite = CCSprite::createWithSpriteFrameName(unitNormal[0]);
        itemWidth = sprite->getContentSize().width;
    }
    return itemWidth;
}

CrushUnitSprite::CrushUnitSprite()
: m_col(0)
, m_row(0)
, m_imgIndex(0)
, m_isNeedRemove(false)
, m_ignoreCheck(false)
, m_displayMode(DISPLAY_MODE_NORMAL)
, m_attackType(SwordAttack)
, m_isJelly(false)
, m_isBomb(false)
{
}

CrushUnitSprite *CrushUnitSprite::create(int row, int col)
{
    CrushUnitSprite *unit = new CrushUnitSprite();
    unit->m_row = row;
    unit->m_col = col;
    unit->m_imgIndex = CCRANDOM_0_1() * TOTAL_UNIT;
    unit->m_attackType = getAttackType(unit->m_imgIndex);
    unit->initWithSpriteFrameName(unitNormal[unit->m_imgIndex]);
    unit->autorelease();
    return unit;
}

CrushUnitSprite *CrushUnitSprite::create(int row, int col, int maxTypeNum)
{
	if(maxTypeNum > TOTAL_UNIT || maxTypeNum < 2){
		maxTypeNum = TOTAL_UNIT;
	}
    CrushUnitSprite *unit = new CrushUnitSprite();
    unit->m_row = row;
    unit->m_col = col;
    unit->m_imgIndex = CCRANDOM_0_1() *maxTypeNum;
    unit->m_attackType = getAttackType(unit->m_imgIndex);
    unit->initWithSpriteFrameName(unitNormal[unit->m_imgIndex]);
    unit->autorelease();
    return unit;
}

CrushUnitSprite *CrushUnitSprite::createJelly(int row, int col)
{
    CrushUnitSprite *unit = new CrushUnitSprite();
    unit->m_row = row;
    unit->m_col = col;
    unit->m_imgIndex = 5;
    unit->m_attackType = 0;
    unit->m_isJelly = true;
    unit->initWithSpriteFrameName(unitNormal[unit->m_imgIndex]);
    unit->autorelease();
    return unit;
}

CrushUnitSprite *CrushUnitSprite::createBomb(int row, int col)
{
    CrushUnitSprite *unit = new CrushUnitSprite();
    unit->m_row = row;
    unit->m_col = col;
    unit->m_imgIndex = 6;
    unit->m_attackType = getAttackType(unit->m_imgIndex);
    unit->m_isBomb = true;
    unit->initWithSpriteFrameName(unitNormal[unit->m_imgIndex]);
    unit->autorelease();
    return unit;
}

int CrushUnitSprite::getAttackType(int index)
{
    int type = 0;
    switch(index){
        case 0:
            type = SwordAttack;
            break;
        case 1:
            type = ArrowAttack;
            break;
        case 2:
            type = FireAttack;
            break;
        case 3:
            type = LightAttack;
            break;
        case 4:
            type = HealthAttack;
            break;
        case 6:
            type = BombAttack;
            break;
        default:
            type = SwordAttack;
            break;
    }
    return type;
}

void CrushUnitSprite::setDisplayMode(DisplayMode mode)
{
    m_displayMode = mode;
    
    SpriteFrame *frame;
    switch (mode) {
        case DISPLAY_MODE_VERTICAL:
            frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(unitVertical[m_imgIndex]);
            break;
        case DISPLAY_MODE_HORIZONTAL:
            frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(unitHorizontal[m_imgIndex]);
            break;
        case DISPLAY_MODE_CIRCLE:
            frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(unitCircle[m_imgIndex]);
            break;
        case DISPLAY_MODE_ALL:
            frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(unitNormal[7]);
            m_imgIndex = 7;
            break;
        default:
            return;
    }
    setDisplayFrame(frame);
}
